package com.dao.impl;

import com.dao.SongService;
import com.entity.Song;
import com.entity.Songer;
import com.entity.Style;
import com.entity.vo.SongVo;
import com.util.JdbcUtil;
import com.util.PageUtil;
import com.util.SingletonThreadLocal;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 歌曲 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2022-03-23
 */
public class SongServiceImpl  implements SongService {

	@Override
	public int insert(Song song) throws SQLException {
		String sql = " insert song (songer_id, name, type, image_url, language, song_url, song_description, lyric, is_deleted, create_time, update_time) values (?,?,?,?,?,?,?,?,?,?,?)";
		Object[] objecs = new Object[]{song.getSongId(), song.getName(), song.getType(), song.getImageUrl(), song.getLanguage(),
				song.getSongUrl(), song.getSongDescription(), song.getLyric(),0, new Date(), new Date()};
		return JdbcUtil.insertOrUpdateOrDelete(sql, objecs);
	}

	@Override
	public List<Song> queryData(Song song) throws IllegalAccessException, SQLException, InstantiationException {
		return null;
	}

	@Override
	public int update(Song song) throws SQLException {
		Object[] objects = new Object[]{song.getSongerId(), song.getName(), song.getType(), song.getImageUrl(), song.getLanguage(), song.getSongUrl(), song.getSongDescription(), song.getLyric(),new Date(), song.getSongId()};
		String sql = "update song set songer_id = ?, name = ?, type = ?, image_url = ?,language = ?, song_url = ?, song_description = ?, lyric = ? , update_time = ? where song_id = ? ";

		return JdbcUtil.insertOrUpdateOrDelete(sql, objects);
	}

	@Override
	public int deleteById(Object id) throws SQLException {
		String sql = "update song set is_deleted = 1 where song_id = ? ";

		return JdbcUtil.insertOrUpdateOrDelete(sql, new Object[]{id});
	}

	@Override
	public PageUtil<Song> queryDataBypage(int currentPgae, int pageSize, Song song) throws IllegalAccessException, SQLException, InstantiationException {
		return null;
	}

	/**
	 * 添加一个歌手名字
	 * @param songerName
	 * @return
	 */
	@Override
	public int addSonger(String songerName) throws SQLException, InstantiationException, IllegalAccessException {
		// 1. 查询是否存在该歌手
		String sql = "select * from songer where is_deleted = 0 and nickname = ?";
		List<Songer> songers = JdbcUtil.queryData(sql, Songer.class, new Object[]{songerName});
		if(!songers.isEmpty()) {
			// 返回第一个歌手
			throw new RuntimeException("歌手存在");
		}

		// 2.添加
		sql = "insert songer (nickname,is_deleted,create_time, update_time) values (?,?,?,?) ";
		int songerId = JdbcUtil.insertReturnAutoinCrementsId(sql, new Object[]{songerName, 0, new Date(), new Date()});

		// 3.返回歌手id   // 先提交当前事务再进行回滚
		return songerId;
	}

	/**
	 * 添加一个风格
	 * @param styleName
	 * @return
	 */
	@Override
	public int addStyle(String styleName) throws SQLException, InstantiationException, IllegalAccessException {
		// 1. 查询是否存在该风格
		String sql = "select * from style where is_deleted = 0 and style_name = ?";
		List<Style> styleList = JdbcUtil.queryData(sql, Style.class, new Object[]{styleName});
		if(!styleList.isEmpty()) throw new RuntimeException("该风格"+styleName+"存在");

		// 2.添加
		sql = "insert style (style_name,is_deleted,create_time, update_time) values (?,?,?,?) ";
		int styleId = JdbcUtil.insertReturnAutoinCrementsId(sql, new Object[]{styleName, 0, new Date(), new Date()});

		// 3.返回风格id   // 先提交当前事务再进行回滚
		return styleId;
	}

	/**
	 * // 模糊分页查询歌手/歌名搜索
	 * @param pageNum
	 * @param pageSize
	 * @param blur
	 * @return
	 */
	@Override
	public PageUtil<SongVo> queryDataBlur(int pageNum, int pageSize, String blur) throws SQLException, InstantiationException, IllegalAccessException {
		String sql = " select song.*,songer.nickname from song inner join songer on song.songer_id = songer.songer_id and song.is_deleted = 0 where songer.nickname like ? or song.`name` like ?  ";
		return JdbcUtil.queryDataByPage(sql, SongVo.class, pageNum, pageSize, new Object[]{"%"+blur+"%","%"+blur+"%"});
	}

}
